package cl.indumotora.induapprecepcion.dao;

import com.sybase.mobile.Application;
import com.sybase.mobile.ConnectionProperties;
import com.sybase.mobile.RegistrationStatus;
import com.sybase.persistence.LoginCredentials;
import com.sybase.persistence.SynchronizationGroup;

import android.util.Log;
import cl.indumotora.induapprecepcion.helper.Constantes;
import cl.indumotora.induapprecepcion.mbo.IndumotoraRecepcionDB;


public class DAO {

	public DAO(Application app) {
		// TODO Auto-generated constructor stub
		IndumotoraRecepcionDB.setApplication(app);
        IndumotoraRecepcionDB.getSynchronizationProfile().setServerName(Constantes.SUP_HOSTDEV); // Convenience only
        
        ConnectionProperties connProps = app.getConnectionProperties();
        LoginCredentials loginCredentials = new LoginCredentials(Constantes.SUP_USERNAME, Constantes.SUP_PASSWORDDEV);
        connProps.setLoginCredentials(loginCredentials);
        
        connProps.setServerName(Constantes.SUP_HOSTDEV);
        connProps.setPortNumber(Constantes.SUP_PORT_INT);
		
        if (app.getRegistrationStatus() != RegistrationStatus.REGISTERED){
            try {
                app.registerApplication(500);
                Log.d("Info", "Complete register");
            } catch (Exception e) {
                Log.d("Error", "Error register: " + e);
            }
        }
        else {
            try {
                app.startConnection(500);
                Log.d("Info", "Complete start");
            } catch (Exception e) {
                Log.d("Error", "Error start: " + e);
            }
        }
        
       
	}
	
	public static void abrirConexionBD() {
		try {
			//CREAMOS LA BD SI NO EXISTE
			IndumotoraRecepcionDB.deleteDatabase();
			if (!IndumotoraRecepcionDB.databaseExists()) {
				IndumotoraRecepcionDB.createDatabase();
			}

			//ABRIENDO LA BD
			IndumotoraRecepcionDB.openConnection();
		} catch (Exception e) {
			Log.i("Exception", "Error: clase DAO; metodo: abrirConexionBD");
		}
	}
	
	
	public static void cerrarConexionBD() {
		try {
			//CERRANDO LA BD
			IndumotoraRecepcionDB.closeConnection();
		} catch (Exception e) {
			Log.i("Exception", "Error: clase DAO; metodo: abrirConexionBD");
		}
	}

}
